package it.enea.xlab.tebes.validation;

import it.enea.xlab.tebes.validation.ValidationController;
import it.enea.xlab.tebes.validation.WebControllersUtilities;
import it.enea.xlab.tebes.validation.common.PropertiesUtil;
import it.enea.xlab.tebes.validation.tester.ValidationTester;

import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xlab.file.XLabFileManager;

import schemavalidator.SchemaMainValidator;
import schemavalidator.SchemaMainValidatorImpl;
import validator.ErrorMessage;

/**
 * @author Cristiano Novelli
 */
public class ValidationManagerImplITCase {

	// Interfaces Declarations
	static ValidationController validationManagerController;

	// Logger
	private static Logger logger = Logger.getLogger(ValidationManagerImplITCase.class);

	@BeforeClass
	public static void before() throws Exception {


	}


	
	@Test
	public void test() {

		logger.info("**************************************");
		logger.info("*** Validation Tester (Standalone) ***");
		logger.info("**************************************");
		logger.info("");
		
		
		logger.info("1) ValidationController Retrieving...");
		validationManagerController = (ValidationController) WebControllersUtilities.getManager(ValidationController.CONTROLLER_NAME);
		Assert.assertNotNull(validationManagerController);	
		logger.info("OK ValidationController!");
		logger.info("");
		
		
		
		logger.info("2) File System Info Retrieving...");
		// i.e. "Validation_Artifacts"
		String validationArtifactsDir = PropertiesUtil.getArtifactsDirProperty();
		logger.info("OK Validation Directory Name: " + validationArtifactsDir);

		// Absolute Path
		String validationArtifactsAbsDirPath = PropertiesUtil.getArtifactsDirPath();
		boolean fileExists = XLabFileManager.isFileOrDirectoryPresent(validationArtifactsAbsDirPath);
		Assert.assertTrue(fileExists);
		logger.info("OK Validation Directory Path: " + validationArtifactsAbsDirPath);

		// i.e. "Validation_Artifacts/ubl-invoice.xml"
		String xml = validationArtifactsDir.concat("/ubl-invoice.xml");
		String schema = "http://winter.bologna.enea.it/peppol_schema_rep/xsd/maindoc/UBL-Invoice-2.0.xsd";
		logger.info("XML: " + xml);
		logger.info("SCHEMA: " + schema);
		logger.info("OK File System Info Retrieving");
		logger.info("");
		
		ErrorMessage emList[] = null;
		
		
		logger.info("3) Set Temporary Directory");
		SchemaMainValidator smv = new SchemaMainValidatorImpl();		
		smv.setRootDir(validationArtifactsDir.concat("/"));
		logger.info("Temporary Directory: " + validationArtifactsDir.concat("/"));
		logger.info("");
		
		try {		
		
			logger.info("4) Validation...");
			
			// VALIDATION
			emList = smv.reportValidation(xml, schema);

		} catch (Exception e) {
			emList = null;
			logger.error("EXCEPTION on reportValidation CALL!");
			logger.error(e.getMessage());
		}

		
		
		if (emList != null) {

			if (emList.length >0) {
				
				int i=0;
				while (i<emList.length){
					
					logger.info("RIGA " + i + ": " + emList[i].getErrorType());
					logger.info("RIGA " + i + ": " + emList[i].getLineNumber());
					logger.info("RIGA " + i + ": " + emList[i].getDescription());
					
					i++;
				}
			}
			logger.info("OK Validation");
		}


	}
	
	
	@AfterClass
	public static void after() throws Exception {
		
	}

	
}




